package SpLib.display
{
	import SpLib.Com.SpLibEvent;
	import SpLib.ObjSys.BaseObj;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	public class Wnd extends SpLib.ObjSys.BaseObj
	{
		private var m_nBorderCol:uint = 0;
		private var m_nActTime:uint = 0;
		
		private var m_nZOrder:int = 0;
		private var m_bShow:Boolean = true;
		private var m_nAlpah:int = 0xFF;
		protected var m_Rect:Rectangle = new Rectangle;
		
		protected var m_szSubList:Array;
		
		public static function onsort(w1:Wnd,w2:Wnd):int
		{
			if(w1.zorder>w2.zorder) return 1;
			if(w1.zorder<w2.zorder) return -1;
		
			if(w1.m_nActTime>w2.m_nActTime) return 1;
			if(w1.m_nActTime<w2.m_nActTime) return -1;

			return 0;
		}
		
		public function Wnd(f:BaseObj)
		{
			super(f);
			
			m_nActTime = getTimer();
			
			RegEvent("SubWndChg",SubWndChg);
		}
		
		override protected function OnAddSubObj():void
		{
			SortSubWnd();
		}
		
		override protected function OnRemoveSubObj():void
		{
			SortSubWnd();
		}
		
		protected function SubWndChg(evt:SpLibEvent):void
		{
			SortSubWnd();
		}
		
		public function Bring2Top():void
		{
			m_nActTime = getTimer();
			DoCmd("father.SubWndChg");
		}
		
		[Bindable]
		public function get show():Boolean
		{
			return m_bShow;
		}
		
		public function set show(s:Boolean):void
		{
			if(s==m_bShow) return;
			m_bShow = s;
			Event("OnShowChg");
		}
		
		[Bindable]
		public function get alpha():int
		{
			return m_nAlpah;
		}
		
		public function set alpha(a:int):void
		{
			m_nAlpah = Math.max(a,0);
			m_nAlpah = Math.min(m_nAlpah,0xFF);
		}
		
		public function get rect():Rectangle
		{
			return m_Rect.clone();
		}
		
		[Bindable]
		public function set x(p:Number):void
		{
			m_Rect.x = p;
		}
		
		public function get x():Number
		{
			return m_Rect.x;
		}
		
		[Bindable]
		public function set y(p:Number):void
		{
			m_Rect.y = p;
		}
		
		public function get y():Number
		{
			return m_Rect.y;
		}
		
		[Bindable]
		public function set width(p:Number):void
		{
			m_Rect.width = p;
		}
		
		public function get width():Number
		{
			return m_Rect.width;
		}
		
		[Bindable]
		public function set height(p:Number):void
		{
			m_Rect.height = p;
		}
		
		public function get height():Number
		{
			return m_Rect.height;
		}
		
		[Bindable]
		public function set zorder(z:int):void
		{
			m_nZOrder = z;
			DoCmd("father.SubWndChg");
		}
		
		public function get zorder():int
		{
			return m_nZOrder;
		}
		
		public function Point2Local(p:Point):Point
		{
			return new Point(p.x-x,p.y-y);
		}
		
		public function toLocalPos(px:Number,py:Number):Point
		{
			return new Point(px-x,py-y);
		}
		
		public function DrawBorder(col:uint = 0):void
		{
			if(m_nBorderCol==col) return;
			m_nBorderCol = col;
		}
		
		public function SortSubWnd():void
		{
			m_szSubList = new Array();
			
			var sub:Wnd = childhead as Wnd;
			while(sub!=null)
			{
				if(sub.show)
				{
					m_szSubList.push(sub);
				}
				
				sub = sub.nextobj as Wnd;
			}
			
			if(m_szSubList.length>1)
			{
				m_szSubList.sort(Wnd.onsort);
			}
		}
		
		public function OnDraw(dc:SpLib.display.Graphics2D):void
		{
			if(!show || dc.RectIsClip(m_Rect)) return;
			
			dc.PushState();
			
			dc.Clip(m_Rect);
			dc.Translate(m_Rect.topLeft);
			
			var drawsub:Boolean = OnDrawSelf(dc);
			
			if(m_nBorderCol!=0)
			{
				dc.Draw_RectBorder(m_nBorderCol);
			}
			
			if(drawsub)
			{
				var sub:Wnd;
				for each(sub in m_szSubList)
				{
					sub.OnDraw(dc);
				}
			}
			
			dc.PopState();
		}
		
		protected function OnDrawSelf(dc:SpLib.display.Graphics2D):Boolean
		{	
			return true;
		}
	}
}